home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 001 / fue / c / DOLOCK < prev    next >
Text File  |  1991-04-03  |  6KB  |  292 lines

  1. /*    DOLOCK.C:    Machine specific code for File Locking
  2. #include    "elang.h"
  3.             for MicroEMACS
  4.             (C)opyright 1987 by Daniel M Lawrence
  5. */
  6.  
  7. #include    "estruct.h"
  8. #include    "etype.h"
  9.  
  10. #if    WMCS
  11. /*    file locking for WMCS */
  12.  
  13. #include "sys$disk/sysincl.sys/sysequ.h"
  14. #include <stdio.h>
  15. #include <ctype.h>
  16.  
  17. char msg[] = TEXT35;
  18. /*           "another user" */
  19.  
  20. char *dolock(fname)
  21. char *fname;
  22. {
  23.     int lun,status;
  24.     status = _open(fname,OPREADACC|OPWRITEACC|OPWRITELOCK,-1,&lun);
  25.     if(status == 133 || status == 0 ) return(NULL);
  26.     return(msg);
  27. }
  28.  
  29. char *undolock(fname)
  30. char *fname;
  31. {
  32.     int i,j,k,lun,status;
  33.     char xname[95],c;
  34.     
  35.     for(lun=4; _getfnam(lun,xname) == 0; lun++) {
  36.         for(i=0;i<strlen(xname);i++)    {
  37.             k = i;
  38.             for(j=0;j<strlen(fname);j++)  {
  39.                 c = fname[j];
  40.                 if(islower(c)) c = toupper(c);
  41.                 if(c == xname[k]) { ++k; continue; }
  42.                 if(c == '\0') break;
  43.                 break;
  44.                 }
  45.             if(j == strlen(fname)) {
  46.                 _close(lun,0);
  47.                 return(NULL);
  48.                 }
  49.             }
  50.     }
  51.     return(NULL);
  52. }
  53. #endif
  54.  
  55. #if    0
  56. /*    dolock:    MDBS specific Unix 4.2BSD file locking mechinism
  57.         this is not to be distributed generally        */
  58.  
  59. #include    <mdbs.h>
  60. #include    <mdbsio.h>
  61. #include    <sys/types.h>
  62. #include    <sys/stat.h>
  63.  
  64. /* included by port.h: mdbs.h, mdbsio.h, sys/types.h, sys/stat.h */
  65.  
  66.  
  67. #ifndef bsdunix
  68. char *dolock(){return(NULL);}
  69. char *undolock(){return(NULL);}
  70. #else
  71.  
  72. #include <pwd.h>
  73. #include <errno.h>
  74.  
  75. extern int errno;
  76.  
  77. #define LOCKDIR ".xlk"
  78.  
  79. #define LOCKMSG TEXT36
  80. /*              "LOCK ERROR -- " */
  81. #define LOCKMSZ sizeof(LOCKMSG)
  82. #define LOCKERR(s) { strcat(lmsg,s); oldumask = umask(oldumask); return(lmsg); }
  83.  
  84. /**********************
  85.  *
  86.  * dolock -- lock the file fname
  87.  *
  88.  * if successful, returns NULL 
  89.  * if file locked, returns username of person locking the file
  90.  * if other error, returns "LOCK ERROR: explanation"
  91.  *
  92.  * Jon Reid, 2/19/86
  93.  *
  94.  *********************/
  95.  
  96. BOOL parent = FALSE;
  97. BOOL tellall = FALSE;
  98.  
  99. char *gtname(filespec)        /* get name component of unix-style filespec */
  100. char *filespec;
  101. {
  102.     char *rname, *rindex();
  103.  
  104.     rname = rindex(filespec,'/');
  105.  
  106.     if (rname != NULL)
  107.         return(rname);
  108.     else
  109.         return(filespec);
  110. }
  111.  
  112. char *getpath(filespec)
  113. char *filespec;
  114. {
  115.     char rbuff[LFILEN];
  116.     char *rname, *rindex();
  117.  
  118.     strcpy(rbuff,filespec);
  119.     rname = rindex(rbuff,'/');
  120.  
  121.     if (rname == NULL)
  122.         return(NULL);
  123.     else
  124.     {
  125.         *(++rname) = '\0';
  126.         return(rbuff);
  127.     }
  128.  
  129. }
  130.  
  131. char *dolock(fname)
  132.     char *fname;
  133. {
  134.     static char lockname[LFILEN] = LOCKDIR;
  135.     static char username[12];
  136.     static char lmsg[40] = LOCKMSG;
  137.     char *pathfmt;
  138.     struct stat statblk;
  139.     struct passwd *pblk;
  140.     long pid, getpid();
  141.     FILE *lf, *fopen();
  142.     int oldumask;
  143.  
  144.     oldumask = umask(0);    /* maximum access allowed to lock files */
  145.  
  146.  
  147.       if (*fname != '/')
  148.        pathfmt = "./%s%s";
  149.       else
  150.        pathfmt = "%s/%s";
  151.       sprintf(lockname,pathfmt,getpath(fname), LOCKDIR);
  152.  
  153.       if (tellall) printf(TEXT37,lockname);
  154. /*                            "checking for existence of %s\n" */
  155.  
  156.       if (stat(lockname,&statblk))
  157.       {
  158.          if (tellall) printf(TEXT38,lockname);
  159. /*                                   "making directory %s\n" */
  160.          mkdir(lockname,0777); 
  161.       }
  162.  
  163.       sprintf(lockname,"%s/%s",lockname,gtname(fname));
  164.  
  165.       if (tellall) printf(TEXT37,lockname);
  166. /*                            "checking for existence of %s\n" */
  167.  
  168.       if (stat(lockname,&statblk))
  169.       {
  170. makelock:      if (tellall) printf(TEXT39,lockname);
  171. /*                                  "creating %s\n" */
  172.  
  173.         if ((lf = fopen(lockname,FOP_TW)) == NULL)
  174.           LOCKERR(TEXT40)
  175. /*                        "could not create lock file" */
  176.             else
  177.           {
  178.             if (parent)
  179.              pid = getppid();    /* parent pid */
  180.             else
  181.              pid = getpid();    /* current pid */
  182.  
  183.              if (tellall)
  184.               printf(TEXT41,pid); 
  185. /*                               "pid is %ld\n" */
  186.  
  187.              fprintf(lf,"%ld",pid); /* write pid to lock file */
  188.  
  189.             fclose(lf);
  190.             oldumask = umask(oldumask);
  191.             return(NULL);
  192.         }
  193.       }
  194.       else
  195.       {
  196.         if (tellall) printf(TEXT42,lockname);
  197. /*                                  "reading lock file %s\n" */
  198.         if ((lf = fopen(lockname,FOP_TR)) == NULL)
  199.           LOCKERR(TEXT43)
  200. /*                        "could not read lock file" */
  201.             else
  202.           {
  203.             fscanf(lf,"%ld",&pid); /* contains current pid */
  204.             fclose(lf);
  205.             if (tellall)
  206.              printf(TEXT44,lockname, pid);
  207. /*                              "pid in %s is %ld\n" */
  208.             if (tellall)
  209.              printf(TEXT45, pid);
  210. /*                              "signaling process %ld\n" */
  211.             if (kill(pid,0))
  212.                 switch (errno)
  213.                 {
  214.                   case ESRCH:    /* process not found */
  215.                         goto makelock;
  216.                         break;
  217.                   case EPERM:    /* process exists, not yours */
  218.                          if (tellall) 
  219.                          puts(TEXT46);
  220. /*                                                    "process exists" */
  221.                         break;
  222.                   default:
  223.                     LOCKERR(TEXT47)
  224. /*                                              "kill was bad" */
  225.                     break;
  226.                 }
  227.             else
  228.              if (tellall) puts(TEXT48);
  229. /*                                         "kill was good; process exists" */
  230.         }
  231.         if ((pblk = getpwuid(statblk.st_uid)) == NULL)
  232.           sprintf(username,"uid %d",asc_int(statblk.st_uid));
  233.         else
  234.           strcpy(username,pblk->pw_name);
  235.  
  236.         oldumask = umask(oldumask);
  237.         return(username);
  238.       }
  239. }
  240.  
  241. /**********************
  242.  *
  243.  * undolock -- unlock the file fname
  244.  *
  245.  * if successful, returns NULL 
  246.  * if other error, returns "LOCK ERROR: explanation"
  247.  *
  248.  * Jon Reid, 2/19/86
  249.  *
  250.  *********************/
  251.  
  252. char *undolock(fname)
  253.     char *fname;
  254. {
  255.     static char lockname[LFILEN] = LOCKDIR;
  256.     static char lmsg[40] = LOCKMSG;
  257.     char *pathfmt;
  258.  
  259.       if (*fname != '/')
  260.        pathfmt = "./%s%s";
  261.       else
  262.        pathfmt = "%s/%s";
  263.       sprintf(lockname,pathfmt,getpath(fname), LOCKDIR);
  264.  
  265.       sprintf(lockname,"%s/%s",lockname,gtname(fname));
  266.  
  267.       if (tellall) printf(TEXT49,lockname);
  268. /*                            "attempting to unlink %s\n" */
  269.  
  270.       if (unlink(lockname))
  271.       { 
  272.         strcat(lmsg,TEXT50); 
  273. /*                          "could not remove lock file" */
  274.         return(lmsg); 
  275.       }
  276.       else
  277.             return(NULL);
  278. }
  279.  
  280. #endif
  281.  
  282. /******************
  283.  * end dolock module
  284.  *******************/
  285.  
  286. #else
  287. dolhello()
  288. {
  289. }
  290. #endif
  291.  
  292.